home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xpaint-2.1.1 / pencilOp.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  5KB  |  167 lines

  1. /* +-------------------------------------------------------------------+ */
  2. /* | Copyright 1992, 1993, David Koblas (koblas@netcom.com)            | */
  3. /* |                                                                   | */
  4. /* | Permission to use, copy, modify, and to distribute this software  | */
  5. /* | and its documentation for any purpose is hereby granted without   | */
  6. /* | fee, provided that the above copyright notice appear in all       | */
  7. /* | copies and that both that copyright notice and this permission    | */
  8. /* | notice appear in supporting documentation.  There is no           | */
  9. /* | representations about the suitability of this software for        | */
  10. /* | any purpose.  this software is provided "as is" without express   | */
  11. /* | or implied warranty.                                              | */
  12. /* |                                                                   | */
  13. /* +-------------------------------------------------------------------+ */
  14.  
  15. #include <X11/Intrinsic.h>
  16. #include <X11/StringDefs.h>
  17. #include <X11/cursorfont.h>
  18. #include "xpaint.h"
  19. #include "misc.h"
  20. #include "Paint.h"
  21.  
  22. typedef struct {
  23.     Boolean    isDots;
  24.     int    startX, startY;
  25.     GC    gc;
  26. } LocalInfo;
  27.  
  28. static void    press(Widget w, LocalInfo *l, XButtonEvent *event, OpInfo *info) 
  29. {
  30.     XRectangle    undo;
  31.  
  32.     if (info->surface == opWindow)
  33.         return;
  34.  
  35.     if ((event->state & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask)) != 0)
  36.         return;
  37.  
  38.     l->startX = event->x;
  39.     l->startY = event->y;
  40.  
  41.     undo.x      = event->x;
  42.     undo.y      = event->y;
  43.     undo.width  = 1;
  44.     undo.height = 1;
  45.  
  46.     if (event->button == Button2)
  47.         l->gc = info->second_gc;
  48.     else
  49.         l->gc = info->first_gc;
  50.     if (l->isDots) {
  51.         GC    gc = XCreateGC(XtDisplay(w), info->drawable, 0, 0);
  52.         XCopyGC(XtDisplay(w), l->gc, ~GCLineWidth, gc);
  53.         l->gc = gc;
  54.     }
  55.  
  56.     UndoStartPoint(w, info, event->x, event->y);
  57.  
  58.     XDrawLine(XtDisplay(w), info->drawable, l->gc,
  59.              l->startX, l->startY, event->x, event->y);
  60.     if (!info->isFat)
  61.         XDrawLine(XtDisplay(w), XtWindow(w), l->gc,
  62.              l->startX, l->startY, event->x, event->y);
  63.  
  64.     PwUpdate(w, &undo, False);
  65. }
  66.  
  67. static void    motion(Widget w, LocalInfo *l, XMotionEvent *event, OpInfo *info) 
  68. {
  69.     XRectangle    undo;
  70.  
  71.     if (info->surface == opWindow)
  72.         return;
  73.  
  74.     if (l->isDots) {
  75.         l->startX = event->x;
  76.         l->startY = event->y;
  77.     }
  78.  
  79.     XDrawLine(XtDisplay(w), info->drawable,l->gc,
  80.              l->startX, l->startY, event->x, event->y);
  81.     if (!info->isFat)
  82.         XDrawLine(XtDisplay(w), XtWindow(w), l->gc,
  83.              l->startX, l->startY, event->x, event->y);
  84.  
  85.     UndoGrow(w, event->x, event->y);
  86.  
  87.     undo.x      = MIN(l->startX, event->x);
  88.     undo.y      = MIN(l->startY, event->y);
  89.     undo.width  = MAX(l->startX, event->x) - undo.x + 1;
  90.     undo.height = MAX(l->startY, event->y) - undo.y + 1;
  91.  
  92.     l->startX = event->x;
  93.     l->startY = event->y;
  94.  
  95.     PwUpdate(w, &undo, False);
  96. }
  97. static void    release(Widget w, LocalInfo *l, XButtonEvent *event, OpInfo *info) 
  98. {
  99.         int     mask;
  100.         /*
  101.         **  Check to make sure all buttons are up, before doing this
  102.         */
  103.         mask = Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask;
  104.         switch (event->button) {
  105.         case Button1:   mask ^= Button1Mask; break;
  106.         case Button2:   mask ^= Button2Mask; break;
  107.         case Button3:   mask ^= Button3Mask; break;
  108.         case Button4:   mask ^= Button4Mask; break;
  109.         case Button5:   mask ^= Button5Mask; break;
  110.         }
  111.         if ((event->state & mask) != 0)
  112.                 return;
  113.  
  114.     if (l->isDots) {
  115.         XFreeGC(XtDisplay(w), l->gc);
  116.         l->gc = None;
  117.     }
  118. }
  119.  
  120. /*
  121. **  Those public functions
  122. */
  123. void *PencilAdd(Widget w)
  124. {
  125.     LocalInfo    *l = (LocalInfo*)XtMalloc(sizeof(LocalInfo));
  126.  
  127.     l->isDots = False;
  128.     XtVaSetValues(w, XtNcompress, False, NULL);
  129.  
  130.     OpAddEventHandler(w, opPixmap, ButtonPressMask, FALSE, (OpEventProc)press, l);
  131.     OpAddEventHandler(w, opPixmap, ButtonMotionMask, FALSE, (OpEventProc)motion, l);
  132.     OpAddEventHandler(w, opPixmap, ButtonReleaseMask, FALSE, (OpEventProc)release, l);
  133.  
  134.     SetPencilCursor(w);
  135.  
  136.     return l;
  137. }
  138. void PencilRemove(Widget w, LocalInfo *l)
  139. {
  140.     OpRemoveEventHandler(w, opPixmap, ButtonPressMask, FALSE, (OpEventProc)press, l);
  141.     OpRemoveEventHandler(w, opPixmap, ButtonMotionMask, FALSE, (OpEventProc)motion, l);
  142.     OpRemoveEventHandler(w, opPixmap, ButtonReleaseMask, FALSE, (OpEventProc)release, l);
  143.     XtFree((XtPointer)l);
  144. }
  145. void *DotPencilAdd(Widget w)
  146. {
  147.     LocalInfo    *l = (LocalInfo*)XtMalloc(sizeof(LocalInfo));
  148.  
  149.     l->isDots = True;
  150.     XtVaSetValues(w, XtNcompress, True, NULL);
  151.  
  152.     OpAddEventHandler(w, opPixmap, ButtonPressMask, FALSE, (OpEventProc)press, l);
  153.     OpAddEventHandler(w, opPixmap, ButtonMotionMask, FALSE, (OpEventProc)motion, l);
  154.     OpAddEventHandler(w, opPixmap, ButtonReleaseMask, FALSE, (OpEventProc)release, l);
  155.  
  156.     SetPencilCursor(w);
  157.  
  158.     return l;
  159. }
  160. void DotPencilRemove(Widget w, LocalInfo *l)
  161. {
  162.     OpRemoveEventHandler(w, opPixmap, ButtonPressMask, FALSE, (OpEventProc)press, l);
  163.     OpRemoveEventHandler(w, opPixmap, ButtonMotionMask, FALSE, (OpEventProc)motion, l);
  164.     OpRemoveEventHandler(w, opPixmap, ButtonReleaseMask, FALSE, (OpEventProc)release, l);
  165.     XtFree((XtPointer)l);
  166. }
  167.